package com.cmcc.aregister.ui.activity;

import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.cmcc.aregister.Controller;
import com.cmcc.aregister.MyApplication;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.http.HttpAsyncTask;
import com.cmcc.aregister.cs.model.UpdateEntity;
import com.unionPay.data.BindPay;
import com.unionPay.data.SaxHandler;
import com.unionPay.data.SaxParser;
import com.unionPay.net.HttpTask;
import com.unionPay.net.TaskExecutor;
import com.unionPay.utils.GlobalInfo;
import com.unionPay.utils.Utils;

public class ParentActivity extends LoginParentActivity {

	UpdateEntity entity = new UpdateEntity();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	/**
	 * 检查更新对话框
	 * 
	 * @param title
	 * @param msg
	 * @param posTitle
	 * @param nagativeTitle
	 */
	public void showAlertDlg_update(String title, String msg, String posTitle,
			String nagativeTitle) {
		LayoutInflater factory = LayoutInflater.from(this);
		final View view = factory.inflate(R.layout.dialog_view_layout, null);
		TextView markTxt = (TextView) view.findViewById(R.id.mark_txt);
		markTxt.setText(msg);
		new AlertDialog.Builder(this)
				.setTitle(title)
				.setIcon(android.R.drawable.ic_dialog_info)
				.setCancelable(false)
				.setView(view)
				.setPositiveButton(posTitle,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								positiveDoworkUpdate();
							}
						})
				.setNegativeButton(nagativeTitle,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								nagativeDoWorkUpdate();
							}

						}).create().show();
	}

	public void positiveDoworkUpdate() {

	}

	public void nagativeDoWorkUpdate() {
		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		Uri content_url = Uri.parse(entity.DownLink);
		intent.setData(content_url);
		startActivity(intent);
	}

	public void addBankCardActivity() {
		new GetOrder().execute();
	}

	class GetOrder extends AsyncTask<String, Integer, BindPay> {
		@Override
		protected BindPay doInBackground(String... params) {
			GlobalInfo.merchantUserId = ConfigUtilHelper.getMobile(app);
			String content = Utils.toMGwXml(GlobalInfo.merchantId,
					Utils.getSendTime(), GlobalInfo.merchantOrderAmt,
					GlobalInfo.merchantUserId);
			try {
				content = Utils.encode(GlobalInfo.merchantId, content);
			} catch (Exception e) {
				e.printStackTrace();
			}

			TaskExecutor taskExecutor = new TaskExecutor();
			HttpTask httpTask = Utils.getHttpTask(GlobalInfo.REQUST_URL,
					content);
			GlobalInfo.currentHttpTask = httpTask;
			taskExecutor.execute(httpTask);
			int index = 0;

			while (GlobalInfo.netResult == null) {
				if (GlobalInfo.currentHttpTask == null) {
					GlobalInfo.currentHttpTask = null;
					return null;
				}
				try {
					if (index > 30) {
						break;
					}
					index++;
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			if (GlobalInfo.netResult == null) {
				return null;
			}

			if (!Utils.decode(GlobalInfo.netResult)) {
				GlobalInfo.netResult = null;
				return null;
			}

			SaxHandler saxHandler = (SaxHandler) SaxParser.parserxml(
					GlobalInfo.netResult, new SaxHandler());
			BindPay bindPay = saxHandler.getBinPay();
			GlobalInfo.netResult = null;
			return bindPay;
		}

		@Override
		protected void onPostExecute(BindPay result) {
			if (result == null) {
				Toast.makeText(ParentActivity.this, "访问服务器失败，请重试！",
						Toast.LENGTH_LONG).show();
			} else {
				if ("0000".equals(result.getRespCode())) {
					GlobalInfo.bindpay = result;
					com.bypay.utils.Utils.setPackageName("com.cmcc.aregister");
					Intent intent = new Intent("yinlian_bindpay");
					Bundle mBundle = new Bundle();
					mBundle.putString("pkg","unionPay_bind_back");
					mBundle.putString("xml", getXml());
					intent.putExtras(mBundle);
					startActivityForResult(intent, 0000);
				} else {
					Toast.makeText(ParentActivity.this, result.getRespDesc(),
							Toast.LENGTH_LONG).show();
				}
			}
			super.onPostExecute(result);
		}

	}

	public String getXml() {
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
				+ "<BindPay sendSeqId=\"" + "1.0.0" + "\">" + "<merchantId>"
				+ GlobalInfo.bindpay.getMerchantId()
				+ "</merchantId>"
				+ "<merchantOrderId>"
				+ GlobalInfo.bindpay.getMerchantOrderId()
				+ "</merchantOrderId>"
				+ "<merchantOrderTime>"
				+ GlobalInfo.bindpay.getMerchantOrderTime()
				+ "</merchantOrderTime>"
				+ "<merchantOrderAmt>"
				+ GlobalInfo.bindpay.getMerchantOrderAmt()
				+ "</merchantOrderAmt>"
				+ "<merchantUserId>"
				+ GlobalInfo.bindpay.getMerchantUserId()
				+ "</merchantUserId>"
				+ "<mobileNum>"
				+ GlobalInfo.userMobile
				+ "</mobileNum>"
				+ "<gwInvokeCmd>"
				+ GlobalInfo.bindpay.getGwInvokeCmd()
				+ "</gwInvokeCmd>"
				+ "<backUrl>"
				+ "http://www.baidu.com"
				+ "</backUrl>"
				+ "<idNum>"
				+ GlobalInfo.userIdCard
				+ "</idNum>"
				+ "<cardNum>"
				+ GlobalInfo.cardNum
				+ "</cardNum>"
				+ "<transType>02</transType>"
				+ "<gwType>"
				+ "04"
				+ "</gwType>"
				+ "<userName>"
				+ GlobalInfo.userName
				+ "</userName>"
				+ "</BindPay>";
		return xml;
	}
}
